//
//  ViewController.m
//  MessageChat
//
//  Created by 王木木 on 2018/1/12.
//  Copyright © 2018年 Mr.H. All rights reserved.
//

#import "ViewController.h"
#import "MessageFunctionBtn.h"
#import "Masonry.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width /**< 屏幕宽度 */
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height /**< 屏幕高度 */
#define textfiledX 50.0
#define messageHeight 50.0
#define multiViewHeight 210.0
@interface ViewController ()
<UITableViewDataSource,
UITableViewDelegate,
UITextFieldDelegate>

@property (strong,nonatomic)NSMutableArray * messages;

@property (strong,nonatomic)UITableView *tableView;


/**
 toolbar
 */
@property (strong,nonatomic)UIView *messageView;


/**
 更多功能栏
 */
@property (strong,nonatomic)UIView *multiView;


/**
 聊天框
 */
@property (strong,nonatomic)UITextField *textField;

/**
 语音文字
 */
@property (strong,nonatomic)UIButton *voiceBtn;

/**
 文字更多
 */
@property (strong,nonatomic)UIButton *textBtn;


/**
 按住说话
 */
@property (strong,nonatomic)UIButton *sendBtn;

@property (nonatomic, strong) MessageFunctionBtn *funtionbtn;

@property (nonatomic, strong) UIImageView *tempView;

@end

@implementation ViewController

- (UIImageView *)tempView {
    
    if (!_tempView) {
        
        _tempView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        
        _tempView.backgroundColor = [UIColor redColor];
        
        [_tempView setHidden:YES];
        
    }
    
    return _tempView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    self.messages = [[NSMutableArray alloc]initWithObjects:@"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     @"1",
                     nil];

    [self.view addSubview:self.tableView];

    [self.view addSubview:self.messageView];

    [self.messageView addSubview:self.textField];
    
    [self.messageView addSubview:self.voiceBtn];
    
    [self.messageView addSubview:self.textBtn];
    
    [self.textField addSubview:self.sendBtn];
    
    [self.view addSubview:self.multiView];
    
    [self.multiView addSubview:self.funtionbtn];
    
    [self.funtionbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(36);
        make.top.mas_equalTo(14);
        make.width.mas_equalTo(55);
        make.height.mas_equalTo(85);
    }];
    
    
    //注册 键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    
    [self.view addSubview:self.tempView];
    
}

#pragma mark - 键盘 改变通知
-(void)keyboardWasChange:(NSNotification *)notification
{
    NSDictionary *info = [notification userInfo];
    
    if ([[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y != SCREEN_HEIGHT) {
        
        [UIView animateWithDuration:0.3 animations:^{
        
        self.messageView.frame = CGRectMake(0, [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y-messageHeight, SCREEN_WIDTH, messageHeight);
        
            NSLog(@"%f",self.messageView.frame.origin.y);
            
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.messageView.frame.origin.y);
        [self scrollToBottom];
            
            }];
        
    }else {
        [self multiViewShow]; 
    }
    
}

-(void)multiViewShow
{
    [UIView animateWithDuration:0.3 animations:^{
        self.multiView.frame = CGRectMake(0, SCREEN_HEIGHT-multiViewHeight, SCREEN_HEIGHT, multiViewHeight);
        
        NSLog(@"%f",self.multiView.frame.origin.y);
        
        self.messageView.frame = CGRectMake(0, self.multiView.frame.origin.y-messageHeight, SCREEN_WIDTH, messageHeight);
        
        NSLog(@"%f",self.messageView.frame.origin.y);
        
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.messageView.frame.origin.y);
        [self scrollToBottom];
    }];
}

-(void)scrollToBottom
{
    NSInteger count = self.messages.count;
    if (count > 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    [self.textField resignFirstResponder];
    
    [self multiViewHidden];
    
    self.voiceBtn.selected = NO;
    
    self.textBtn.selected = NO;
    
}

-(void)multiViewHidden
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.multiView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_HEIGHT, multiViewHeight);
        self.messageView.frame = CGRectMake(0, SCREEN_HEIGHT-messageHeight, SCREEN_WIDTH, messageHeight);
        self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.messageView.frame.origin.y);
    }];
}

/*
 *  按下录音按钮开始录音
 */
- (void)recordButtonTouchDown
{
    NSLog(@"按下");
  
    [self.tempView setHidden:NO];
    
}

/*
 *  手指向上滑动取消录音
 */
- (void)recordButtonTouchUpOutside
{
    
    NSLog(@"外部离开");
    
    
}

/*
 *  松开手指完成录音
 */
- (void)recordButtonTouchUpInside
{
  
    NSLog(@"在内部离开");
    
    [self.tempView setHidden:YES];
    
}

/*
 *  当手指离开按钮的范围内时，主要为了通知外部的HUD
 */
- (void)recordDragOutside
{
   
    NSLog(@"已经离开按钮");
    
}

/*
 *  当手指再次进入按钮的范围内时，主要也是为了通知外部的HUD
 */
- (void)recordDragInside
{
  
    NSLog(@"按下5");
    
}

/**
 点击语音
 */
- (void)touchVoiceBtn {
    
    if (!self.voiceBtn.isSelected) {
        
        [_sendBtn setHidden:NO];
        
        [self.textField resignFirstResponder];
        [self multiViewHidden];
        NSLog(@"隐藏键盘");
        
        self.textBtn.selected = NO;
        
    }else {
      
        NSLog(@"显示键盘");
        
        [self.textField becomeFirstResponder];
        
        [_sendBtn setHidden:YES];
        
    }
    
    
    self.voiceBtn.selected = !self.voiceBtn.isSelected;
    
}

/**
 点击更多
 */
- (void)touchTextBtn {
    
    self.voiceBtn.selected = NO;
    
    [_sendBtn setHidden:YES];
    
     if (!self.textBtn.isSelected) {
         
         [self.textField resignFirstResponder];
         [self multiViewShow];
         self.textBtn.selected = YES;
         
     }else {
         
         [self.textField becomeFirstResponder];
         
         self.textBtn.selected = NO;
         
     }
    
}

- (MessageFunctionBtn *)funtionbtn {
    
    if (!_funtionbtn) {
        
        _funtionbtn = [[MessageFunctionBtn alloc]init];
        
        [_funtionbtn setImageName:@"case" titleName:@"相册"];
        
    }
    
    return _funtionbtn;
    
}

- (UIButton *)sendBtn {
    
    if (!_sendBtn) {
        
        CGRect frame = CGRectMake(0, 0, self.textField.frame.size.width, self.textField.frame.size.height);
        
        _sendBtn = [[UIButton alloc]initWithFrame:frame];
        
        [_sendBtn setTitle:@"按住说话" forState:UIControlStateNormal];

        [_sendBtn setBackgroundImage:[UIImage imageNamed:@"case"] forState:UIControlStateNormal];
        
        [_sendBtn addTarget:self action:@selector(recordButtonTouchDown) forControlEvents:UIControlEventTouchDown];
        [_sendBtn addTarget:self action:@selector(recordButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
        [_sendBtn addTarget:self action:@selector(recordButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [_sendBtn addTarget:self action:@selector(recordDragOutside) forControlEvents:UIControlEventTouchDragExit];
        [_sendBtn addTarget:self action:@selector(recordDragInside) forControlEvents:UIControlEventTouchDragEnter];
        
        [_sendBtn setHidden:YES];
        
    }
    
    return _sendBtn;
    
    
}

- (UIButton *)voiceBtn {
    
    if (!_voiceBtn) {
        
        CGRect frame = CGRectMake(0, 0, 50, 50);
        
        _voiceBtn = [[UIButton alloc]initWithFrame:frame];
        
        [_voiceBtn setImageEdgeInsets:UIEdgeInsetsMake(12.5, 12.5, 12.5, 12.5)];
        
        [_voiceBtn setImage:[UIImage imageNamed:@"chatBar_record"] forState:UIControlStateNormal];
        
        [_voiceBtn setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateSelected];
        
        [_voiceBtn addTarget:self action:@selector(touchVoiceBtn) forControlEvents:UIControlEventTouchUpInside];
        
        
        
    }
    
    return _voiceBtn;
}

- (UIButton *)textBtn {
    
    if (!_textBtn) {
        
        CGFloat width = self.textField.frame.size.width + textfiledX;
        
        CGRect frame = CGRectMake(width, 0, 50, 50);
        
        _textBtn = [[UIButton alloc]initWithFrame:frame];
        
        [_textBtn setImageEdgeInsets:UIEdgeInsetsMake(12.5, 12.5, 12.5, 12.5)];
        
        [_textBtn setImage:[UIImage imageNamed:@"chatBar_more"] forState:UIControlStateNormal];
        
        [_textBtn setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateSelected];
        
             [_textBtn addTarget:self action:@selector(touchTextBtn) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return _textBtn;
    
    
}

- (UITextField *)textField {
    
    if (!_textField) {
        
        //输入框
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(textfiledX, 10, self.messageView.frame.size.width - textfiledX * 2, 32)];
        
        _textField.delegate = self;
        
        _textField.returnKeyType = UIReturnKeySend;
        
        _textField.enablesReturnKeyAutomatically = YES;
        
        _textField.borderStyle = UITextBorderStyleRoundedRect;
        
    }
    
    return _textField;
    
}

- (UIView *)messageView {
    
    if (!_messageView) {
        
        _messageView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-messageHeight, SCREEN_WIDTH, messageHeight)];

        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];

        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

        effectView.frame = _messageView.bounds;

        [_messageView addSubview:effectView];
        
        UILabel *labelTop = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        
        labelTop.backgroundColor = [UIColor greenColor];
        
        [_messages insertObject:labelTop atIndex:10];
        
    }
    
    return _messageView;
}

- (UIView *)multiView {
    
    if (!_multiView) {
        
        _multiView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, multiViewHeight)];
        
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        
        effectView.frame = _multiView.bounds;
        
        _multiView.backgroundColor = [UIColor colorWithRed:248/255.0 green:247/255.0 blue:247/255.0 alpha:1.0];
        
        [_multiView addSubview:effectView];
        
    }
    
    return _multiView;
    
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        //消息显示
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -44) style:UITableViewStylePlain];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.backgroundColor = [UIColor darkGrayColor];
        
        _tableView.delegate = self;
        
        _tableView.dataSource = self;
        
        _tableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        
    }
    
    return _tableView;
    
}

#pragma mark -- tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messages.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    cell.backgroundColor = [UIColor clearColor];
    
    return cell;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    if ([textField.text isEqualToString:@""]) {
        return YES;
    }
    
    [self.messages addObject:textField.text];
    
    [self.tableView reloadData];
    
    [self scrollToBottom];
    
    textField.text = @"";
    
    return YES;
}

- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures {
    
    return UIRectEdgeBottom;
    
}


@end
